﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Interactivity;
using System.Windows.Shapes;

namespace JHMVVMFramework.Common.Behavior
{
    public class RichTextBoxContentBehavior :Behavior<RichTextBox>
    {
        public static DependencyProperty ContentProperty
            = DependencyProperty.RegisterAttached(
            "Content", typeof(object),
            typeof(RichTextBoxContentBehavior),
            new PropertyMetadata(null, OnContentChanged));

        public bool IsEnbale { get; set; }

        public object Content
        {
            set
            {
                SetValue(ContentProperty, value);
            }
            get
            {
                return (string)GetValue(ContentProperty);
            }
        }

        private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RichTextBoxContentBehavior sender = d as RichTextBoxContentBehavior;
            object content = e.NewValue;

            sender.AssociatedObject.Blocks.Clear();
            BlockCollection newBlockCollection = sender.ConvertContent(content);
            for (int i = 0; i < newBlockCollection.Count; i++)
            {
                sender.AssociatedObject.Blocks.Add(newBlockCollection[i]);
            }
           
        }

        protected virtual BlockCollection ConvertContent(object content)
        {
            throw new NotImplementedException("This method must be inplemented");
        }

    }
}
